﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Account_UserList : BasePage
{
    #region 属性
    private string StrWhere
    {
        get
        {
            return ViewState["StrWhere"] == null ? "" : ViewState["StrWhere"].ToString();
        }
        set
        {
            ViewState["StrWhere"] = value;
        }
    }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        base.CheckUserPermission("001004");

        if (!IsPostBack)
        {
            //设置分页控件文本
            setLanguage();

            //绑定下拉框
            BindDropDownList();

            //绑定数据
            BindRepeaterData("1=1");
        }
    }

    /// <summary>
    /// 设置分页控件文本
    /// </summary>
    private void setLanguage()
    {
        TextUtility.SetPaginateCtlText(PaginateCtl);
    }
    #endregion

    #region 数据绑定
    private void BindRepeaterData(string swhere)
    {
        int pageIndex = PaginateCtl.CurrentPageIndex;
        int pageSize = PaginateCtl.PageSize;
        UtilityBLL dbu = new UtilityBLL();

        //绑定数据源
        if (base.CurrentSession.UserName != "SystemManager")
        {
            swhere += " and UserName<>'SystemManager'";
        }

        RepeaterList.DataSource = dbu.GetListByPage("view_user_user", "*", swhere, "userid", 0, pageIndex, pageSize);
        //返回记录条数
        PaginateCtl.RecordCount = dbu.GetRecordCount("view_user_user", swhere);
        //设置记录总数的显示文本(此方法必须在设置PaginateCtl.RecordCount之后调用)
        TextUtility.SetPaginateCtlRecoText(PaginateCtl);
        RepeaterList.DataBind();
    }

    private void BindDropDownList()
    {
        List<sys_role> list = new List<sys_role>();
        sys_roleBLL bll = new sys_roleBLL();
        list = bll.GetModelList("Valid=1");
        this.drop_Role.DataTextField = "RoleName";
        this.drop_Role.DataValueField = "RoleId";
        this.drop_Role.DataSource = list;
        this.drop_Role.DataBind();

        this.drop_Role.Items.Insert(0, new ListItem("--请选择--", "0"));
    }
    #endregion

    #region 查询
    /// <summary>
    /// 查询按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string swhere = "1=1";
        if (this.drop_Role.SelectedIndex > 0)
        {
            swhere += " and RoleId=" + TextUtility.ReplaceText(this.drop_Role.SelectedValue) + "";
        }
        if (this.txt_UserName.Text.Trim().Length > 0)
        {
            swhere += " and UserName like '%" + TextUtility.ReplaceText(this.txt_UserName.Text.Trim()) + "%'";
        }
        if (this.txt_LoginName.Text.Trim().Length > 0)
        {
            swhere += " and LoginName like '%" + TextUtility.ReplaceText(this.txt_LoginName.Text.Trim()) + "%'";
        }
        StrWhere = swhere;
        //分页控件回到第1页
        PaginateCtl.CurrentPageIndex = 1;
        BindRepeaterData(StrWhere);
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label validText = (Label)e.Item.FindControl("ValidText");
            LinkButton Lbvalid = (LinkButton)e.Item.FindControl("BtnOperate");

            Lbvalid.Text = "修改";
            //设置启用禁用的显示文本
            if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
            {
                validText.Text = "禁用";
                validText.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                validText.Text = "启用";
                validText.ForeColor = System.Drawing.Color.Green;
            }
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Valid")
        {
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                sys_userBLL bll = new sys_userBLL();
                sys_user model = new sys_user();
                model = bll.GetModel(id);
                model.Valid = model.Valid == 0 ? 1 : 0;
                bll.Update(model);
                BindRepeaterData(StrWhere);
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }
        }
        else if (e.CommandName == "Reset")
        {
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                sys_userBLL bll = new sys_userBLL();
                sys_user model = new sys_user();
                model = bll.GetModel(id);
                model.LoginPwd = "123456";
                bll.Update(model);

                base.Redirect("修改密码成功！新密码为：123456");
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }
        }
    }
    #endregion

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PaginateCtl_PageChanged(object sender, EventArgs e)
    {
        BindRepeaterData(StrWhere);
    }
    #endregion
}
